package com.concise.appapi.service;

import cn.hutool.core.convert.Convert;
import com.concise.appapi.service.model.ArchiveModel;
import com.initialize.pub.dao.ArticleDAO;
import com.initialize.pub.generate.dao.LabelMapper;
import com.initialize.pub.generate.pojo.Article;
import com.initialize.pub.generate.pojo.Label;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/3/6
 * Time: 9:51 下午
 * Description:
 */
@Slf4j
@Service
public class PigeonholeService {

    final
    ArticleDAO articleDAO;

    public PigeonholeService(ArticleDAO articleDAO) {
        this.articleDAO = articleDAO;
    }

    public int getTheNumberOfArticles() {
        return articleDAO.selALlNum();
    }

    @Autowired
    LabelMapper labelMapper;

    public List<ArchiveModel> getList() {
        Collection<Article> aListOfArticles =
                articleDAO.getAListOfArticles();
        return dataToModel(aListOfArticles);
    }

    private List<ArchiveModel> dataToModel(Collection<Article> aListOfArticles) {
        List<ArchiveModel> archiveModels = new ArrayList<>();
        for (Article aListOfArticle : aListOfArticles) {
            ArchiveModel archiveModel = ArchiveModel.builder()
                    .aid(aListOfArticle.getAid())
                    .time(aListOfArticle.getReleaseTime())
                    .title(aListOfArticle.getTitle())
                    .build();
            String label = aListOfArticle.getLabel();
            if (!label.trim().isEmpty()) {
                String[] split = label.split("\\|");
                log.info("label==>{}", label);
                List<String> stringList = new ArrayList<>();
                for (String s : split) {
                    Label label1 = labelMapper.selectByPrimaryKey(Convert.toInt(s));
                    stringList.add(label1.getName());
                }
                archiveModel.setLabelList(stringList);
            }
            archiveModels.add(archiveModel);
        }
        return archiveModels;
    }

}